package com.flashflexpro.flexdirector.agent.scriptrunner{
    import com.flashflexpro.flexdirector.adapter.CodecHelper;
    import com.flashflexpro.flexdirector.adapter.descriptor.DescPropertyAuto;
    import com.flashflexpro.flexdirector.agent.FlexDirectorAgent;
    import com.flashflexpro.flexdirector.agent.console.Console;


    import flash.events.EventDispatcher;
    import flash.system.System;

    import mx.automation.Automation;
    import mx.automation.AutomationID;
    import mx.automation.IAutomationClass;
    import mx.core.UIComponent;

    public class ScriptRunnerImpl extends EventDispatcher implements IScriptRunner{

        public function ScriptRunnerImpl(){
        }

        public static const NOT_FOUND:String = "Target UI not found ";

        private var _lastNotFoundId:String;
        public function get lastNotFoundId():String{
            return _lastNotFoundId;
        }

        //>>>
        public function findUIComponent( autoId:String ):UIComponent{
            //todo: clean this duplicate code
            var aid:AutomationID = AutomationID.parse( autoId );
            var autoObj:UIComponent = FlexDirectorAgent.inst.findAutoObjByAutoIdStr( aid ) as UIComponent;

            if( autoObj == null ){
                _lastNotFoundId = autoId;
                throw new Error( NOT_FOUND );
            }

            var nid:AutomationID = Automation.automationManager.createID( autoObj );
            if( !aid.equals( nid ) ){
                warn( { type:"newAutoId", oldId:autoId, newId:nid.toString() } );
            }
            return autoObj;
        }

        /*
         public function getMemberOf (autoObj:UIComponent, memberName:String):* {
         var a:IAutomationClass = Automation.automationManager.automationEnvironment.getAutomationClassByInstance (autoObj);
         var desc:IAutomationPropertyDescriptor = a.propertyNameMap[ memberName ] as IAutomationPropertyDescriptor;
         var val:* = null;
         try {
         val = FlexDirectorAutoAdapter.getCodecHelper().propertyCodecMap (desc.codecName).encode (autoObj, desc, autoObj).value;
         }
         catch(e:Error) {
         val = CodecDefault.getMemberFromObject (autoObj, desc);
         trace (e.getStackTrace ());
         }
         return val;
         }*/

        public function runAtomAct( obj:UIComponent, actName:String, args:Array ):void{
            if( !Automation.automationManager.showInHierarchy( obj ) ){
                warn( "component instance is not visible!" );
            }
            FlexDirectorAgent.replayStep( obj, actName, args );
        }


        public function log( ...rest ):void{
            trace( rest );
        }

        public function warn( ...rest ):void{
            trace( rest );
        }

        public function error( ...rest ):void{
            trace( rest );
        }

        //<<<

        private var _lastMemberNamePD:DescPropertyAuto;
        public function get lastMemberNamePD():DescPropertyAuto{
            return _lastMemberNamePD;
        }

        public function getMemberOf( obj:UIComponent, memberName:String ):*{
            var a:IAutomationClass = Automation.automationManager.automationEnvironment
                    .getAutomationClassByInstance( obj );
            _lastMemberNamePD = a.propertyNameMap[ memberName ];
            return CodecHelper.getPropertyValue( obj, _lastMemberNamePD ).value;
        }


        public function exit():void{
            Console.inst.exit();
            try{
                System.exit( 0 );
            }
            catch( e:Error ){
                try{
                    FlexDirectorAgent.sysManager.stage.loaderInfo.loader.unload();
                }
                catch( e:Error ){
                    trace( e.getStackTrace() );
                }
            }
        }

        public function runProcedure( procedure:IUsingScriptRunner ):void{
            new RunningProcedure( procedure ).run();
        }
    }
}